﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Core;
using Tesla.Graphics;
using Tesla.Math;

namespace Tesla.Scene.Shape {
    /// <summary>
    /// A mesh that represents a quad that by default faces toward +Z. Its height
    /// is along the y-axis, and width along the x-axis. The geometry is indexed and
    /// each vertex contains a Position, Normal, and Texture coordinate by default.
    /// </summary>
    public class Quad : Mesh {
        private float _width;
        private float _height;

        /// <summary>
        /// Gets the width of the quad
        /// </summary>
        public float Width {
            get {
                return _width;
            }
        }

        /// <summary>
        /// Gets the height of the quad
        /// </summary>
        public float Height {
            get {
                return _height;
            }
        }

        /// <summary>
        /// Creates a new quad. Height and width will be 1.
        /// </summary>
        /// <param name="name">Name of the quad</param>
        public Quad(String name) 
            : base(name) {
                _width = 1;
                _height = 1;

                MeshData.UseDynamicIndexBuffer = false;
                MeshData.UseDynamicVertexBuffer = false;
                MeshData.UseIndexedPrimitives = true;
                DataBuffer<Vector3> positions = new DataBuffer<Vector3>(4);
                SetPositionData(positions);
                SetNormalData();
                SetTextureData();
                SetIndexData();
                MeshData.Reconstruct();
                UpdateModelBound();
        }

        /// <summary>
        /// Creates a new quad.
        /// </summary>
        /// <param name="name">Naame of the quad</param>
        /// <param name="width">Width of the quad</param>
        /// <param name="height">Height of the quad</param>
        public Quad(String name, float width, float height) 
            : base(name) {
            _width = width;
            _height = height;

            MeshData.UseDynamicIndexBuffer = false;
            MeshData.UseDynamicVertexBuffer = false;
            MeshData.UseIndexedPrimitives = true;
            DataBuffer<Vector3> positions = new DataBuffer<Vector3>(4);
            SetPositionData(positions);
            SetNormalData();
            SetTextureData();
            SetIndexData();
            MeshData.Reconstruct();
            UpdateModelBound();
        }

        /// <summary>
        /// Resizes the quad. Does not call meshData.Reconstruct()
        /// </summary>
        /// <param name="width">Width of the quad</param>
        /// <param name="height">Height of the quad</param>
        public void Resize(float width, float height) {
            _width = width;
            _height = height;
            DataBuffer<Vector3> verts = MeshData.Positions;
            SetPositionData(verts);
            UpdateModelBound();
        }

        private void SetPositionData(DataBuffer<Vector3> positions) {
            positions.Position = 0;
            positions.Set(new Vector3(-_width / 2, _height / 2, 0));
            positions.Set(new Vector3(_width / 2, _height / 2, 0));
            positions.Set(new Vector3(_width / 2, -_height / 2, 0));
            positions.Set(new Vector3(-_width / 2, -_height / 2, 0));

            MeshData.UpdateVertexData<Vector3>(VertexSemantic.Position, positions);
        }

        private void SetNormalData() {
            DataBuffer<Vector3> normals = new DataBuffer<Vector3>(4);
            normals.Set(Vector3.UnitZ);
            normals.Set(Vector3.UnitZ);
            normals.Set(Vector3.UnitZ);
            normals.Set(Vector3.UnitZ);

            MeshData.UpdateVertexData<Vector3>(VertexSemantic.Normal, normals);
        }

        private void SetTextureData() {
            DataBuffer<Vector2> texCoords = new DataBuffer<Vector2>(4);
            texCoords.Set(new Vector2(0, 0));
            texCoords.Set(new Vector2(1, 0));
            texCoords.Set(new Vector2(1, 1));
            texCoords.Set(new Vector2(0, 1));

            MeshData.UpdateVertexData<Vector2>(VertexSemantic.TextureCoordinate, texCoords);
        }

        private void SetIndexData() {
            short[] temp = new short[] { 0, 1, 2, 0, 2, 3 };
           // short[] temp = new short[] { 0, 2, 1, 0, 3, 2 };
            DataBuffer<short> indices = new DataBuffer<short>(temp);

            MeshData.UpdateIndexData<short>(indices);
        }
    }
}
